<template>
  <div class="space-category">
    <a-card title="空间图片分类">
      <v-chart :option="options" style="height: 320px; max-width: 100%" :loading="loading" />
    </a-card>
  </div>
</template>
<script setup lang="ts">
import 'echarts'
import VChart from 'vue-echarts'
import { computed, ref, watchEffect } from 'vue'
import { getSpaceCategoryAnalyzeUsingPost } from '@/api/spaceAnalyzeController.ts'
import { message } from 'ant-design-vue'

interface Porps {
  queryAll?: boolean
  queryPublic?: boolean
  spaceId?: string
}

const props = withDefaults(defineProps<Porps>(), {
  queryAll: false,
  queryPublic: false,
})
const dataList = ref<API.SpaceCategoryAnalyzeVO[]>([])
const loading = ref(false)
const fetchData = async () => {
  loading.value = true
  try {
    const res = await getSpaceCategoryAnalyzeUsingPost({
      queryAll: props.queryAll,
      queryPublic: props.queryPublic,
      spaceId: props.spaceId,
    })
    console.log(res.data)
    dataList.value = res.data
  } catch (e) {
    message.error(e)
  } finally {
    loading.value = false
  }
}
// 图表选项
const options = computed(() => {
  const categories = dataList.value.map((item) => item.category)
  const countData = dataList.value.map((item) => item.count)
  const sizeData = dataList.value.map((item) => (item.totalSize / 1024 / 1024).toFixed(2))
  return {
    tooltip: {
      trigger: 'axis',
    },
    legend: {
      data: ['图片数量', '图片总大小'],
      top: 'bottom',
    },
    xAxis: {
      type: 'category',
      data: categories,
    },
    yAxis: [
      {
        type: 'value',
        name: '图片数量',
        axisLine: { show: true, lineStyle: { color: '#5470c6' } },
      },
      {
        type: 'value',
        name: '图片总大小(MB)',
        position: 'right',
        axisLine: { show: true, lineStyle: { color: '#91CC75' } },
        splitLine: {
          lineStyle: {
            color: '#91CC75',
            type: 'dashed',
          },
        },
      },
    ],
    series: [
      { name: '图片数量', type: 'bar', data: countData, yAxisIndex: 0 },
      { name: '图片总大小', type: 'bar', data: sizeData, yAxisIndex: 1 },
    ],
  }
})
// 加载数据
watchEffect(() => {
  fetchData()
})
</script>

<style scoped></style>
